package com.GamersCom.games;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.GamersCom.db.Platform;
import com.GamersCom.model.MgrFactory;
import com.GamersCom.model.PlatformMgr;
import com.GamersCom.util.GlobalCONST;
import com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException;

/**
 * Servlet implementation class AdminPlatforms
 */
public class AdminPlatforms extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		process(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		process(request, response);
	}

	protected void process(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		HttpSession session = (HttpSession) request.getSession();
		String nextView = "/platform.jsp";
		String username = (String) session.getAttribute("username");

		// redirect the user to login
		if (username == null)
		{
			nextView = "/login.jsp";
		}
		else
		{
			int group = (Integer)session.getAttribute("accesslevel");

			if (group != GlobalCONST.ADMINISTRATOR)
			{
				request.setAttribute("error", "Restricted Area!");
				nextView = "/result.jsp";

			}
			else
			{
				MgrFactory factory = (MgrFactory) this.getServletContext()
						.getAttribute("mgrFactory");
				PlatformMgr pfMgr = factory.getPlatformMgr();

				// pasing the action
				String submit = request.getParameter("submit");

				if ( submit != null )
				{
					
					// Add Action
					if (submit.equals("Add"))
					{
						// retrieve the form data
						Platform pf = new Platform();
						pf.setName(request.getParameter("platform"));

						// validate the input 
						if ( pfMgr.nameExists(pf.getName()))
						{
							request.setAttribute("msg", "Name existes!");
						}
						else
						{
							try
							{
								pfMgr.insertPlatform(pf);
								request.setAttribute("msg", "Add successfully!");
								
								// change the game submenu
//								List<Platform> list = pfMgr.getAll();
//								this.getServletContext().setAttribute("gameSubMenu", list);
							}
							catch (SQLException e)
							{
								// TODO Auto-generated catch block
								e.printStackTrace();
								request.setAttribute("msg", "Add failed!");
							}	
						}
						

					}
					
					// Delete Action
					if ( submit.equals("Delete")) 
					{
						Platform pf = new Platform();
						pf.setName(request.getParameter("pf"));
						
						try
						{
							pfMgr.delete(pf);
							request.setAttribute("msg", "Delete successfully!");
							
							
						}
						catch (SQLException e)
						{
							// TODO Auto-generated catch block
							e.printStackTrace();
							request.setAttribute("msg", "Error: Delete failed!");
						}						
					}
					
					// Update Action
					if ( submit.equals("Update"))
					{
						Platform pf = new Platform();						
						pf.setName(request.getParameter("pf"));
						Platform pf2 = new Platform();
						pf2.setName(request.getParameter("pfNew"));
												
						/* update the field with same value but difference cases; 
						 * Not the same value, display error if name exists, otherwise update.
						 */
						if ( ( ! pf.getName().toLowerCase().equals(pf2.getName().toLowerCase()) ) && pfMgr.nameExists(pf2.getName()) )
						{
							request.setAttribute("msg", "Error: name existes!");
						}
						else
						{
							try
							{
								pfMgr.update(pf, pf2);
								request.setAttribute("msg", "Update successfully!");
							}
							catch (SQLException e)
							{
								e.printStackTrace();
								System.out.println("Do you catch me???");
								request.setAttribute("msg", "Error: Update failed!");
							}	
						}
						
					}
					
				}
				
				//Get the platform List
				List<Platform> list;
				try
				{
					list = pfMgr.getAll();
					request.setAttribute("pfList", list);
					this.getServletContext().setAttribute("gameSubMenu", list);
				}
				catch (SQLException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
					request.setAttribute("msg", "Error Retrieve Pf list!");
				}				
			}
		}

		request.getRequestDispatcher(nextView).forward(request, response);
	}


}
